package com.jdd.modules.system.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jdd.common.util.oConvertUtils;
import com.jdd.dao.entity.*;
import com.jdd.dao.service.TAreaService;
import com.jdd.modules.device.entity.TDevice;
import com.jdd.modules.device.service.TDeviceService;
import com.jdd.modules.people.model.TProjectTreeModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LS
 * @Date: 2022 - 09 - 20 - 15:07
 * @Description:
 * @version: 1.0
 */
@Slf4j
@Component
public class FindsTprojectChildrenUtil {

    @Resource
    private TAreaService tAreaService;
    @Resource
    private TDeviceService tDeviceService;

    /**
     * queryTreeList的子方法 ====1=====
     * 该方法是s将SysDepart类型的list集合转换成TProjectTreeModel类型的集合
     */
    public  List<TProjectTreeModel> wrapTreeDataToTreeList(List<TProject> recordList, List<TArea> areaList, List<TBuilding> tBuildings, List<TUnit> tUnits, List<TFloor> tFloors) {

        LambdaQueryWrapper<TDevice> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.isNull(TDevice::getBuildingId);
        lambdaQueryWrapper.isNull(TDevice::getUnitId);
        lambdaQueryWrapper.isNull(TDevice::getFloorId);
        List<TDevice> areaDeviceList = tDeviceService.list(lambdaQueryWrapper);
        LambdaQueryWrapper<TDevice> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.isNotNull(TDevice::getBuildingId);
        lambdaQueryWrapper1.isNull(TDevice::getUnitId);
        lambdaQueryWrapper1.isNull(TDevice::getFloorId);
        List<TDevice> buildingDeviceList = tDeviceService.list(lambdaQueryWrapper1);

        LambdaQueryWrapper<TDevice> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.isNotNull(TDevice::getBuildingId);
        lambdaQueryWrapper2.isNotNull(TDevice::getUnitId);
        lambdaQueryWrapper2.isNull(TDevice::getFloorId);
        List<TDevice> unitDeviceList = tDeviceService.list(lambdaQueryWrapper2);

        LambdaQueryWrapper<TDevice> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper3.isNotNull(TDevice::getFloorId);
        List<TDevice> floorDeviceList = tDeviceService.list(lambdaQueryWrapper3);

        List<TProjectTreeModel> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            TProject tProject = recordList.get(i);
            records.add(new TProjectTreeModel(tProject));
        }
        for (int i = 0; i < areaList.size(); i++) {
            TArea tArea = areaList.get(i);
            for (TDevice tDevice : areaDeviceList) {
                if (tArea.getId().equals(tDevice.getAreaId())) {
                    records.add(new TProjectTreeModel(tDevice, tArea));
                }
            }
            records.add(new TProjectTreeModel(tArea));
        }
        for (int i = 0; i < tBuildings.size(); i++) {
            TBuilding tBuilding = tBuildings.get(i);
            for (TDevice tDevice : buildingDeviceList) {
                if (tBuilding.getId().equals(tDevice.getBuildingId())){
                    records.add(new TProjectTreeModel(tDevice,tBuilding));
                }
            }
            records.add(new TProjectTreeModel(tBuilding));
        }
        for (int i = 0; i < tUnits.size(); i++) {
            TUnit tUnit = tUnits.get(i);
            for (TDevice tDevice : unitDeviceList) {
                if (tUnit.getId().equals(tDevice.getUnitId())){
                    records.add(new TProjectTreeModel(tDevice,tUnit));
                }
            }
            records.add(new TProjectTreeModel(tUnit));
        }
        for (int i = 0; i < tFloors.size(); i++) {
            TFloor tFloor = tFloors.get(i);
            for (TDevice tDevice : floorDeviceList) {
                if (tFloor.getId().equals(tDevice.getFloorId())){
                    records.add(new TProjectTreeModel(tDevice,tFloor));
                }
            }
            records.add(new TProjectTreeModel(tFloor));
        }
        List<TProjectTreeModel> tree = findChildren(records);
        setEmptyChildrenAsNull(tree);
        return tree;
    }

    /**
     * queryTreeList的子方法 ====2=====
     * 该方法是找到并封装顶级父类的节点到TreeList集合
     */
    private static List<TProjectTreeModel> findChildren(List<TProjectTreeModel> recordList) {
        List<TProjectTreeModel> treeList = new ArrayList<>();
        for (TProjectTreeModel branch : recordList) {
            if (oConvertUtils.isEmpty(branch.getParentId())) {
                treeList.add(branch);
            }
        }
        getGrandChildren(treeList, recordList);
        return treeList;
    }

    /**
     * queryTreeList的子方法====3====
     * 该方法是找到顶级父类下的所有子节点集合并封装到TreeList集合
     */
    private static void getGrandChildren(List<TProjectTreeModel> treeList, List<TProjectTreeModel> recordList) {
        for (TProjectTreeModel model : treeList) {
            for (TProjectTreeModel m : recordList) {
                if (m.getParentId() != null && m.getParentId().equals(model.getId())) {
                    model.getChildren().add(m);
                }
            }
            getGrandChildren(model.getChildren(), recordList);
        }

    }


    /**
     * queryTreeList的子方法 ====4====
     * 该方法是将子节点为空的List集合设置为Null值
     */
    private static void setEmptyChildrenAsNull(List<TProjectTreeModel> treeList) {
        for (TProjectTreeModel model : treeList) {
            if (model.getChildren().isEmpty()) {
                model.setChildren(null);
                model.setLeaf(true);
            } else {
                setEmptyChildrenAsNull(model.getChildren());
                model.setLeaf(false);
            }
        }
    }

}
